const crypto = require('crypto');

let server = function (app, db) {
    //?Cookie查询
    const View_Cookie = function (request, calka) {
        db.query('select * from login where UserCookie=\'' + request.cookies.DOM_Cookie + '\'', function (err, data) {
            calka(err, data)
        })
    }

    //?注册</registered>
    app.post('/regisstered', function (request, response) {
        console.log('select * from user where UserID=' + request.body.UserID);
        var res = /^[0-9]+$/g.test(request.body.UserID)
        if (res) {
            db.query('select * from user where UserID=' + (request.body.UserID), function (err, data) {
                if (err) console.error(err);
                if (data[0] == null) {
                    db.query(
                        'insert user(UserID,UserPassword,UserName) values(' + request.body.UserID + ',\'' + request.body.UserPassword + '\',\'' + request.body.UserName + '\')',
                        function (err, data_) {
                            if (err) {
                                console.error(err);
                                response.send({ "code": -1, "msg": "error" })
                            }
                            else {
                                console.log(data_);
                                const md5 = crypto.createHash('md5');//?生成md5实例（一次性）
                                let time__ = new Date().getTime();//?时间戳
                                const cookie = md5.update(String(parseInt(time__ * 3000))).digest('hex');//?md5加密
                                db.query('update login set UserCookie=\'' + cookie + '\' where UserID=' + request.body.UserID, function (err, data) {
                                    response.cookie('DOM_Cookie', cookie, { maxAge: 60 * 60000 });
                                    response.send({ "code": 0, "msg": "注册成功" })
                                })

                            }
                        })
                } else response.send({ "code": 1, "msg": "账号已注册" })


            })
        }
        else response.send({ "code": -1, "msg": "账号只能为数字" })

    })

    //?login，登录接口</login>
    app.post('/login', function (request, response) {
        if (request.body.UserPassword != null && request.body.UserID != null) {
            // console.log(request.body.UserID);
            db.query('select UserPassword from user where UserID=?', [request.body.UserID], function (err, data) {
                if (err) {
                    console.error(err);
                    response.send({ "code": -1, "msg": "error" })
                }
                else {
                    console.log(data);
                    if (data[0]) {
                        if (data[0]['UserPassword'] == request.body.UserPassword) {
                            const md5 = crypto.createHash('md5');//?生成md5实例（一次性）
                            let time__ = new Date().getTime();//?时间戳
                            const cookie = md5.update(String(parseInt(time__ * 3000))).digest('hex');//?md5加密
                            db.query('update login set UserCookie=\'' + cookie + '\' where UserID=' + request.body.UserID, function (err, data) {
                                response.cookie('DOM_Cookie', cookie, { maxAge: 60 * 60 * 1000 });
                                response.send({ "code": 0, "msg": "登录成功" })
                            })
                        }
                        else response.send({ "code": -1, "msg": "密码错误" })
                    } else response.send({ "code": -1, "msg": "账号不存在" })
                }
            })
        } else response.send({ "code": -1, "msg": "error" })
    })

    //?退出登录
    app.post('/exit', function (request, response) {
        response.clearCookie('DOM_Cookie')
        response.end()
    })

    //?个人信息</user>
    app.post('/UserData', function (request, response) {
        if (request.body.UserID) {
            db.query('select UserID,UserName,MenuNum,FansNum from user where UserID=' + request.body.UserID, function (err, data) {
                if (err) {
                    console.log(err);
                    response.send({ "code": -1, "result": 'error' })
                }
                else if (data[0] != null)
                    response.send({ "code": 0, "result": data[0] })
                else response.send({ "code": -1, "result": 'error' })
            })
        } else
            View_Cookie(request, function (err, data) {
                if (err) {
                    console.log(err);
                    response.send({ "code": -1, "result": 'error' })
                } else {
                    if (data[0] != null) {
                        db.query('select * from user where UserID=' + data[0]['UserID'], function (err, data) {
                            if (err) {
                                console.log(err);
                                response.send({ "code": -1, "result": 'error' })
                            }
                            else if (data[0] != null)
                                response.send({ "code": 0, "result": data[0] })
                            else response.send({ "code": -1, "result": 'error' })
                        })
                    } else response.send({ "code": -1, "result": 'error' })
                }
            })
    })

    //?信息修改
    app.post('/UserName_change', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                db.query('update user set UserName=\'' + request.body.UserName + '\'', function (err, data1) {
                    if (err) {
                        console.error(err);
                        response.send({ "code": -1, "result": 'error' })
                    } else {
                        response.send({ "code": 0, "result": 'OK' })
                    }
                })
            } else response.send({ "code": -1, "result": 'error' })
        })
    })

    //?密码修改
    app.post('/change_pas', function (request, response) {
        View_Cookie(request, function (err, data) {
            if (data[0]) {
                db.query('select * from user where UserID=' + data[0].UserID, function (err, data) {
                    console.log(request.body.old_pas, data[0].UserPassword);
                    if (err) console.error(err);
                    if (request.body.old_pas == data[0].UserPassword) {
                        db.query('update user set UserPassword=? where UserID=?', [request.body.new_pas, data[0].UserID], function (err, data1) {
                            response.send({ "code": 0, "result": '修改成功' })
                        })
                    } else response.send({ "code": 1, "result": '旧密码错误' })
                })

            } else response.send({ "code": -1, "result": 'error' })
        })
    })
}
module.exports = server
